package com.sk.weichat.mvp.presenter;

import android.content.Context;
import android.util.Log;
import android.view.View;

import com.cjt2325.cameralibrary.util.LogUtil;
import com.sk.weichat.bean.VersionInfo;
import com.sk.weichat.mvp.contract.VersionContract;
import com.sk.weichat.ui.base.CoreManager;
import com.sk.weichat.ui.me.AboutActivity;
import com.sk.weichat.util.AppUtils;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class VersionPresenter {

    private VersionContract mVersionContract;
    private CoreManager coreManager;

    public VersionPresenter(CoreManager coreManager,VersionContract mVersionContract) {
        this.mVersionContract = mVersionContract;
        this.coreManager = coreManager;
    }

    /**
     * 获取版本信息
     */
    public void getVersionInfo(){
        Map<String, String> params = new HashMap<>();
        params.put("type", String.valueOf(1));
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        HttpUtils.get().url(coreManager.getConfig().UPDATE_VERSION)
                .params(params)
                .build()
                .execute(new BaseCallback<VersionInfo>(VersionInfo.class) {

                    @Override
                    public void onResponse(ObjectResult<VersionInfo> result) {
                        if (result.getResultCode()==1&& result.getData() != null){

                            mVersionContract.getVersionInfo(result.getData());
                            //判断版本号是否一致 ，一致则不下载，不一致则显示下载更新
//                            if (AppUtils.getAppVersion(context).equals(result.getData().getAppVersion())){
//                                btn_update.setVisibility(View.GONE);
//                            }else {
//                                mVersionInfo = result.getData();
//                                btn_update.setVisibility(View.VISIBLE);
//                            }
                            LogUtil.e(result.getData().getAppVersion());
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        Log.e("VersionInfo", "Exception", e);
                    }
                });
    }
}
